// pages/movie-detail/movie-detail.js
const {
  GbaseUrl
} = getApp()
import {
  toPromise,
  convertTocastString,
  convertTocastInfos
} from '../../utils/index'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    movie: {}
  },
  processMovieData(movie) {
    const data = {}
    // console.log( movie.directors)
    data.directors = convertTocastString(movie.directors)
    data.casts = convertTocastString(movie.casts)
    data.images = movie.images.large
    data.title = movie.title
    data.subtitle = movie.countries + '·' + movie.year
    data.wish_count = movie.wish_count
    data.wish_count = movie.wish_count
    data.rating = movie.rating
    data.genres = movie.genres.join('、')
    data.summary=movie.summary
    data.castsInfo=convertTocastInfos(movie.casts)
    this.setData({
      movie: data
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    const mid = options.mid
    const params = {
      url: `${GbaseUrl}/subject/${mid}`,
      method: 'get',
    }
    let detail = (await toPromise(wx.request)(params))
    // this.setData({
    //   movie: detail.data
    // })
    // console.log(detail.data)
    this.processMovieData(detail.data)
  },


  onViewPost(e) { //相册功能(预览)
    wx.previewImage({
      urls: [this.data.movie.images, this.data.movie.images],
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})